package com.wzm.basic.controller;

import com.wzm.basic.util.AjaxResult;
import com.wzm.basic.util.FastDfsUtils;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @PostMapping
    public AjaxResult upload(@RequestPart("file")MultipartFile file){
        try {
            //获取原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取文件扩展名
            String exName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //调用工具类上传方法
            String fileName = FastDfsUtils.upload(file.getBytes(), exName);
            //将文件名返回给前端
            return AjaxResult.me().setResultObj(fileName);
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"上传失败");
        }
    }

    public AjaxResult del(@RequestParam("path") String path){
        try {
            String pathTemp = path.substring(1);
            String groupName = pathTemp.substring(0, pathTemp.indexOf("/"));
            String filename = pathTemp.substring(pathTemp.indexOf("/"));
            FastDfsUtils.delete(groupName,filename);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
}
